<!--  -->
<template>
  <div id="home">
    <nav-bar class="homenav">
      <div slot="center" :class="{ write: true }">购物车</div>
    </nav-bar>

    <tab-control
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="tabcontrol1"
      class="tab-control"
      v-show="isTabFixed"
    ></tab-control>

    <scroll
      class="homecontent"
      ref="scroll"
      :probe-type="3"
      @scroll="contentscroll"
      :pull-up-load="true"
      @pullingUp="LoadMore"
    >
      <home-swiper
        :banners="banners"
        @swiperImageLoad="swiperImageLoad"
      ></home-swiper>
      <home-recom :recommends="recommends"></home-recom>
      <feature-view></feature-view>
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabcontrol2"
      ></tab-control>
      <goods-list :goods="goods[Currenttype].list"></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="currentflag"></back-top>
  </div>
</template>

<script>
import HomeSwiper from "./Chilcomp/HomeSwiper.vue";
import HomeRecom from "./Chilcomp/HomeRecommend";
import FeatureView from "./Chilcomp/FeatureView.vue";

import NavBar from "../../components/common/navbar/NavBar";
import TabControl from "../../components/content/Tabcontrol/TabControl";
import GoodsList from "../../components/content/Goods/GoodsList";
import Scroll from "../../components/common/scroll/scroll";
import BackTop from "../../components/content/backTop/BackTop";

import { getHomeMultidata, getHomeGoods } from "../../network/home";

export default {
  name: "home",
  components: {
    HomeSwiper,
    HomeRecom,
    FeatureView,
    NavBar,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },
  data() {
    return {
      banners: {},
      recommends: {},
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      Currenttype: "pop",
      currentflag: true,
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    };
  },
  activated() {
    //console.log("active");
    this.$refs.scroll.scroll.scrollTo(0, this.saveY);
    this.$refs.scroll.scroll.refresh();
  },
  deactivated() {
    //console.log("deactive");
    this.saveY = this.$refs.scroll.getScrolY();
  },
  created() {
    //生命周期函数
    //1.请求多个数据
    this.GetHomeMultidata();
    this.GetHomeGood("pop");
    this.GetHomeGood("new");
    this.GetHomeGood("sell");
  },

  methods: {
    tabClick(index) {
      switch (index) {
        case 0:
          this.Currenttype = "pop";
          break;
        case 1:
          this.Currenttype = "new";
          break;
        case 2:
          this.Currenttype = "sell";
      }
      this.$refs.tabcontrol1.currentIndex = index;
      this.$refs.tabcontrol2.currentIndex = index;
    },
    /*网络请求 */
    GetHomeMultidata() {
      getHomeMultidata().then((res) => {
        //console.log(res);
        this.banners = res.data.banner;
        this.recommends = res.data.recommend;
      });
    },
    GetHomeGood(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;

        //console.log(res);
        this.$refs.scroll.finishPullUp();
        this.$refs.scroll.scroll.refresh();
      });
    },
    backClick() {
      this.$refs.scroll.scroll.scrollTo(0, 0, 300);
    },
    contentscroll(position) {
      //console.log(position);
      this.currentflag = -position.y > 1000;
      /*决定是否吸顶 */
      this.isTabFixed = -position.y > this.tabOffsetTop;
    },
    LoadMore() {
      //console.log("qqqq");
      this.GetHomeGood(this.Currenttype);
    },
    swiperImageLoad() {
      //console.log(this.$refs.tabcontrol.$el.offsetTop);
      //console.log("---");
      this.tabOffsetTop = this.$refs.tabcontrol2.$el.offsetTop;
    },
  },
};
</script>
<style scoped>
#home {
  /*   padding-top: 44px; */
  height: 100vh;
  position: relative;
}
.homenav {
  background-color: var(--color-tint);
  /*   position: fixed;
  left: 0px;
  top: 0px;
  right: 0px;
  z-index: 9; */
}
.write {
  color: white;
}
/* .tab-control {
  position: sticky;
  top: 44px;
  background-color: #fff;
  z-index: 9;
} */
.tab-control {
  position: relative;
  z-index: 9;
}
.homecontent {
  /*   height: calc(100vh - 93px); */
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0px;
  right: 0px;
}
/* .fixed {
  position: fixed;
  left: 0;
  right: 0;
  top: 44px;
} */
</style>